package com.hxd.otc.module.my.activity;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.common.RecentLoginAccountInfoHelper;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.dialog.EditAccountInfoDialog;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.my.contract.AccountContract;
import com.hxd.otc.module.my.presenter.AccountInfoPresenter;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.UIUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/11 0011.
 * description ${个人信息界面}
 */
public class AccountInfoActivity extends BaseActivity<AccountInfoPresenter> implements AccountContract.View {
    @BindView(R.id.tv_uid)
    TextView mTvUid;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_nickname)
    TextView mTvNickName;
    @BindView(R.id.fl_uid)
    FrameLayout mFlUid;
    @BindView(R.id.view_nickname)
    View mViewNickName;
    @BindView(R.id.view_uid)
    View mViewUid;

    /**
     * @param type One of
     * {@link EditAccountInfoDialog#MODIFY_TYPE_CHECKPWD},
     * {@link EditAccountInfoDialog#MODIFY_TYPE_NAME}
     * or{@link EditAccountInfoDialog#MODIFY_TYPE_NICKNAME}
     */
    public static final String EXTRA_MODIFY_TYPE = "modify_type";
    private EditAccountInfoDialog mEditNickNameDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_info;
    }

    @Override
    protected AccountInfoPresenter createPresenter() {
        return new AccountInfoPresenter(this);
    }

    @Override
    protected void initView() {
        registerEventBus();
        mTvNickName.setText(String.format(getString(R.string.my_account_nickname_format), UserHelper.getNickName()));
        mTvUid.setText(ResourceUtil.getString(R.string.account_id, UserHelper.getId()));
    }

    @OnClick({R.id.tv_nickname, R.id.tv_name, R.id.btn_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_nickname:
                onModifyNickName();
                break;
            case R.id.tv_name:
                onModifyName();
                break;
            case R.id.btn_logout:
                mPresenter.logout();
                break;
        }
    }

    @Override
    public void logoutSucceed() {
        onLogout();
    }

    @Override
    public void logoutFail(String errMessage) {
        LogUtils.e(errMessage);
        onLogout();
    }

    /**
     * 修改昵称
     */
    protected void onModifyNickName() {
        if (mEditNickNameDialog == null) mEditNickNameDialog = new EditAccountInfoDialog(this);
        mEditNickNameDialog.setModifyType(EditAccountInfoDialog.MODIFY_TYPE_NICKNAME)
                .setDefaultText(UserHelper.getNickName());
        mEditNickNameDialog.show();
    }

    private void onLogout() {
        BaseApplication.isHideZeroAssests = false;
        RecentLoginAccountInfoHelper.logout();
        UserHelper.logout(this);

    }

    protected void onModifyName() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAccountInfoEvent(Event.UserInfoUpdateEvent userInfoUpdateEvent) {
        mTvNickName.setText(String.format(getString(R.string.my_account_nickname_format), UserHelper.getNickName()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mEditNickNameDialog);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }
}
